﻿using ATLASConfNotes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using DeepZoomPivotConstructor.PivotLib;
using System.Collections.Generic;
using t_TestingLibrary;
using System.ComponentModel.Composition;
using PaperRenderingInterfaces;
using System.IO;
using System.Linq;

namespace t_ATLASConfNotes
{
    
    
    /// <summary>
    ///This is a test class for ATLASMasterConfNoteWebPageParseEngineTest and is intended
    ///to contain all ATLASMasterConfNoteWebPageParseEngineTest Unit Tests
    ///</summary>
    [TestClass()]
    public class t_ATLASMasterConfNoteWebPageParseEngine
    {
        [TestInitialize]
        public void TestSetup()
        {
            Utilities.MyClassInit();
        }

        [TestCleanup]
        public void TestCleanup()
        {
            Utilities.MyClassDone();
        }

        [TestMethod]
        public void TestRenderCTor()
        {
            ATLASMasterConfNoteWebPageParseEngine t = new ATLASMasterConfNoteWebPageParseEngine();
        }

        /// <summary>
        /// Load a file instead of accessing the web.
        /// </summary>
        [Export(typeof(IWebAccess))]
        private class LoadAFile : IWebAccess
        {
            public FileInfo theFileToLoad;

            public Uri WhatWasAskedFor { get; set; }

            public string GetUriAsString(Uri whatToLoad)
            {
                WhatWasAskedFor = whatToLoad;

                if (theFileToLoad.Exists)
                {
                    using (var reader = theFileToLoad.OpenText())
                    {
                        return reader.ReadToEnd();
                    }
                }
                else
                {
                    return null;
                }
                throw new NotImplementedException();
            }


            public DateTime? GetLastModificationTime(Uri whatToCheck)
            {
                throw new NotImplementedException();
            }
        }

        [Export(typeof(IPaperRenderEngine))]
        class DummyRender : IPaperRenderEngine
        {
            /// <summary>
            /// A dummy render. Just return one image.
            /// </summary>
            /// <param name="renderName"></param>
            /// <param name="renderAddress"></param>
            /// <returns></returns>
            public IEnumerable<PivotImage> Render(string renderName, string renderAddress)
            {
                PivotImage img = new PivotImage(null);
                yield return img;
            }
        }

        [TestMethod]
        public void TestMasterConfNotePage()
        {
            ATLASMasterConfNoteWebPageParseEngine t = new ATLASMasterConfNoteWebPageParseEngine();
            Utilities.AddPart(new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_ATLASConfNotes\\atlas-master-confpage.html") });
            Utilities.AddPart(new DummyRender());
            Utilities.Compose(t);

            var result = t.Render("https://atlas.web.cern.ch/Atlas/GROUPS/PHYSICS/CONFNOTES/");

            Assert.AreEqual(15, result.Count(), "Expected to see 15 conference notes referenced here!");
        }

        [TestMethod]
        public void TestConfNotesInterMixed()
        {
            ATLASMasterConfNoteWebPageParseEngine t = new ATLASMasterConfNoteWebPageParseEngine();
            Utilities.AddPart(new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_ATLASConfNotes\\atlas-flavor-confpage.html") });
            Utilities.AddPart(new DummyRender());
            Utilities.Compose(t);

            var result = t.Render("https://twiki.cern.ch/twiki/bin/view/Atlas/FlavourTaggingPublicResultsCollisionData");

            Assert.AreEqual(3, result.Count(), "Expected to see 3 conference notes referenced here!");
        }
    }
}
